<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools
          :tree-node="{
            name: '江苏传智播客教育科技股份有限公司',
            manager: '负责人',
            id: '',
          }"
          :is-root="true"
          @addDeparment="addDeparment"
        />
        <hr>
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <template #default="scoped">
            <TreeTools
              :tree-node="scoped.data"
              @delDeparments="delDeparments"
              @addDeparment="addDeparment"
              @editDeparment="editDeparment"
            />
          </template>
        </el-tree>
      </el-card>
      <addDepart
        ref="deptForm"
        :tree-node="treeNode"
        :show-dialog.sync="showdialog"
        @addSubmit="addSubmit"
      />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/TreeTools'
import addDepart from './components/addDepart'
import { getDeparments } from '@/api/departments'
import { listToTree } from '@/utils/index'
export default {
  components: {
    TreeTools,
    addDepart
  },
  data() {
    return {
      defaultProps: {
        label: 'name'
      },
      departs: [],
      showdialog: false,
      treeNode: ''
    }
  },
  created() {
    this.getDeparments()
  },
  methods: {
    async getDeparments() {
      // console.log((await getDeparments()).depts)
      this.departs = listToTree((await getDeparments()).depts, '')
    },
    delDeparments() {
      this.getDeparments()
    },
    addDeparment(treeNode) {
      this.showdialog = true
      // console.log(treeNode)
      this.treeNode = treeNode
    },
    addSubmit() {
      this.getDeparments()
    },
    editDeparment(treeNode) {
      this.showdialog = true
      this.treeNode = treeNode
      this.$refs.deptForm.getDeparmentsDetail(treeNode.id)
    }
  }
}
</script>

<style>
</style>
